/*
 * Copyright (c) United Binary LLC.  All rights reserved.
 * 
 * This code is licensed under the MIT License
 * 
 * SEE: http://harnessit.codeplex.com/license
 * 
 */
#region using ...
using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
#endregion

namespace UnitedBinary.HarnessIt
{
	/// <summary>
	/// Summary description for TestEngineSelectionForm.
	/// </summary>
	internal class TestEngineSelectionForm : System.Windows.Forms.Form
	{
		#region Member Variables
		private System.Windows.Forms.ListBox listBoxEngines;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label labelEngineName;
		private System.Windows.Forms.Label labelClassName;
		private System.Windows.Forms.Label labelDLL;
		private System.Windows.Forms.Button buttonOK;
		private System.Windows.Forms.Button buttonCancel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private TestEngine.EngineEnumerator engineEnumerator = null;
		private System.Windows.Forms.Button buttonSettings;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label labelCompany;
		private System.Windows.Forms.TextBox textBoxDesc;
		private System.Windows.Forms.GroupBox groupBox2;
		private TestEngine.ITestEngine currentEngine = null;
		#endregion

		#region Construction and Cleanup
		public TestEngineSelectionForm(TestEngine.EngineEnumerator EngineEnumerator, TestEngine.ITestEngine CurrentEngine)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// Add any constructor code after InitializeComponent call
			//
			engineEnumerator = EngineEnumerator;
			currentEngine = CurrentEngine;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TestEngineSelectionForm));
			this.listBoxEngines = new System.Windows.Forms.ListBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textBoxDesc = new System.Windows.Forms.TextBox();
			this.label8 = new System.Windows.Forms.Label();
			this.labelCompany = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.labelDLL = new System.Windows.Forms.Label();
			this.labelClassName = new System.Windows.Forms.Label();
			this.labelEngineName = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.buttonSettings = new System.Windows.Forms.Button();
			this.buttonOK = new System.Windows.Forms.Button();
			this.buttonCancel = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// listBoxEngines
			// 
			this.listBoxEngines.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.listBoxEngines.IntegralHeight = false;
			this.listBoxEngines.Location = new System.Drawing.Point(8, 20);
			this.listBoxEngines.Name = "listBoxEngines";
			this.listBoxEngines.Size = new System.Drawing.Size(584, 97);
			this.listBoxEngines.TabIndex = 0;
			this.listBoxEngines.SelectedIndexChanged += new System.EventHandler(this.listBoxEngines_SelectedIndexChanged);
			// 
			// groupBox1
			// 
			this.groupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.textBoxDesc,
																					this.label8,
																					this.labelCompany,
																					this.label5,
																					this.labelDLL,
																					this.labelClassName,
																					this.labelEngineName,
																					this.label3,
																					this.label2,
																					this.label1,
																					this.buttonSettings});
			this.groupBox1.Location = new System.Drawing.Point(8, 140);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(600, 200);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Selected Engine Details";
			// 
			// textBoxDesc
			// 
			this.textBoxDesc.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.textBoxDesc.Location = new System.Drawing.Point(104, 100);
			this.textBoxDesc.Multiline = true;
			this.textBoxDesc.Name = "textBoxDesc";
			this.textBoxDesc.ReadOnly = true;
			this.textBoxDesc.Size = new System.Drawing.Size(488, 60);
			this.textBoxDesc.TabIndex = 1;
			this.textBoxDesc.Text = "";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(12, 100);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(88, 16);
			this.label8.TabIndex = 6;
			this.label8.Text = "Description";
			// 
			// labelCompany
			// 
			this.labelCompany.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.labelCompany.Location = new System.Drawing.Point(104, 80);
			this.labelCompany.Name = "labelCompany";
			this.labelCompany.Size = new System.Drawing.Size(488, 16);
			this.labelCompany.TabIndex = 2;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(12, 80);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(88, 16);
			this.label5.TabIndex = 7;
			this.label5.Text = "Company:";
			// 
			// labelDLL
			// 
			this.labelDLL.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.labelDLL.Location = new System.Drawing.Point(104, 60);
			this.labelDLL.Name = "labelDLL";
			this.labelDLL.Size = new System.Drawing.Size(488, 16);
			this.labelDLL.TabIndex = 3;
			// 
			// labelClassName
			// 
			this.labelClassName.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.labelClassName.Location = new System.Drawing.Point(104, 40);
			this.labelClassName.Name = "labelClassName";
			this.labelClassName.Size = new System.Drawing.Size(488, 16);
			this.labelClassName.TabIndex = 4;
			// 
			// labelEngineName
			// 
			this.labelEngineName.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.labelEngineName.Location = new System.Drawing.Point(104, 20);
			this.labelEngineName.Name = "labelEngineName";
			this.labelEngineName.Size = new System.Drawing.Size(488, 16);
			this.labelEngineName.TabIndex = 5;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(12, 60);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(88, 16);
			this.label3.TabIndex = 8;
			this.label3.Text = "Assembly:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(12, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 16);
			this.label2.TabIndex = 9;
			this.label2.Text = "Class Name:";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(12, 20);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(88, 16);
			this.label1.TabIndex = 10;
			this.label1.Text = "Engine Name:";
			// 
			// buttonSettings
			// 
			this.buttonSettings.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.buttonSettings.Enabled = false;
			this.buttonSettings.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonSettings.Location = new System.Drawing.Point(516, 168);
			this.buttonSettings.Name = "buttonSettings";
			this.buttonSettings.TabIndex = 0;
			this.buttonSettings.Text = "&Configure";
			this.buttonSettings.Click += new System.EventHandler(this.buttonSettings_Click);
			// 
			// buttonOK
			// 
			this.buttonOK.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.buttonOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonOK.Location = new System.Drawing.Point(436, 348);
			this.buttonOK.Name = "buttonOK";
			this.buttonOK.TabIndex = 2;
			this.buttonOK.Text = "&OK";
			this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
			// 
			// buttonCancel
			// 
			this.buttonCancel.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.buttonCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonCancel.Location = new System.Drawing.Point(524, 348);
			this.buttonCancel.Name = "buttonCancel";
			this.buttonCancel.TabIndex = 3;
			this.buttonCancel.Text = "C&ancel";
			this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.listBoxEngines});
			this.groupBox2.Location = new System.Drawing.Point(8, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(600, 128);
			this.groupBox2.TabIndex = 0;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Available Test Engines";
			// 
			// TestEngineSelectionForm
			// 
			this.AcceptButton = this.buttonOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.buttonCancel;
			this.ClientSize = new System.Drawing.Size(616, 382);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox2,
																		  this.buttonCancel,
																		  this.buttonOK,
																		  this.groupBox1});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimumSize = new System.Drawing.Size(336, 400);
			this.Name = "TestEngineSelectionForm";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "HarnessIt - [Test Execution Engines]";
			this.Load += new System.EventHandler(this.TestEngineSelectionForm_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#region Windows Form Event Handlers
		private void buttonOK_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void buttonCancel_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void TestEngineSelectionForm_Load(object sender, System.EventArgs e)
		{
			for(int i=0; i<EngineEnumerator.AvailableEngines.Length; i++)
			{
				TestEngine.ITestEngine engine = EngineEnumerator.AvailableEngines[i];
				listBoxEngines.Items.Add(engine.Name);
				if (SelectedEngine.Name == engine.Name)
				{
					listBoxEngines.SelectedIndex = i;
				}
			}		
		}

		private void listBoxEngines_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			SetSelectedEngine();

			bool settingsAvailable = false;
			if (SelectedEngine != null)
			{
				labelClassName.Text = SelectedEngine.GetType().FullName;
				labelEngineName.Text = SelectedEngine.Name;
				labelCompany.Text = SelectedEngine.Company;
				textBoxDesc.Text = SelectedEngine.Description;
				labelDLL.Text = GetNameFromAssemblyInfo(SelectedEngine.GetType().Assembly.FullName);
				
				settingsAvailable = SelectedEngine.SettingsDialogAvailable;
			}

			buttonSettings.Enabled = settingsAvailable;
		}

		private void buttonSettings_Click(object sender, System.EventArgs e)
		{
			if (SelectedEngine != null && SelectedEngine.SettingsDialogAvailable)
			{
				if (SelectedEngine.ShowSettingsDialog() == DialogResult.OK)
				{
				}
			}
		}
		#endregion

		#region Engine Helper Methods
		private void SetSelectedEngine()
		{
			if (listBoxEngines.SelectedItem == null)
			{
				return;
			}

			string engineName = listBoxEngines.SelectedItem.ToString();
			Debug.WriteLine("Searching for engine \""+engineName+"\".");

			TestEngine.ITestEngine selectedEngine = null;
			foreach(TestEngine.ITestEngine i in EngineEnumerator.AvailableEngines)
			{
				if (engineName == i.Name)
				{
					selectedEngine = i;
				}
			}

			currentEngine = selectedEngine;
		}

		private string GetNameFromAssemblyInfo(string name)
		{
			int i = name.IndexOf(',',0);
			return name.Substring(0,i);
		}
		#endregion

		#region Engine Properties
		public TestEngine.EngineEnumerator EngineEnumerator
		{
			get
			{
				return engineEnumerator;
			}
		}

		public TestEngine.ITestEngine SelectedEngine
		{
			get
			{
				return currentEngine;
			}
			set
			{
				currentEngine = value;
			}
		}
		#endregion
	}
}
